package io.renren.modules.archive.controller;

import java.util.Arrays;
import java.util.List;
import java.util.Map;

import io.renren.common.utils.StringUtils;
import io.renren.modules.archive.entity.ArchiveCollectEntity;
import io.renren.modules.archive.entity.JgsyCommonEntity;
import io.renren.modules.archive.entity.MeetingtypeEntity;
import io.renren.modules.archive.entity.NdJgMenuEntity;
import io.renren.modules.archive.service.ArchiveCollectService;
import io.renren.modules.archive.service.DictionaryService;
import io.renren.modules.archive.service.MeetingtypeService;
import io.renren.modules.archive.service.NdJgMenuService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;


import io.renren.common.utils.PageUtils;
import io.renren.common.utils.R;


/**
 * ${comments}
 *
 * @author lizeliang
 * @email 2749877017@qq.com
 * @date 2023-08-21 17:43:12
 */
@RestController
@RequestMapping("archive/meetingtype")
public class MeetingtypeController {
    @Autowired
    private MeetingtypeService meetingtypeService;
    @Autowired
    private DictionaryService dictionaryService;
    @Autowired
    private ArchiveCollectService archiveCollectService;
    @Autowired
    private NdJgMenuService ndJgMenuService;

    /**
     * 列表
     */
    @RequestMapping("/list")
//    @RequiresPermissions("archive:meetingtype:list")
    public R list(@RequestParam Map<String, Object> params) {
        PageUtils page = meetingtypeService.queryPage(params);
        List<MeetingtypeEntity> meetingTypeList = (List<MeetingtypeEntity>) page.getList();
        for (MeetingtypeEntity meetingtypeEntity : meetingTypeList
        ) {
            if (StringUtils.isNotBlank(meetingtypeEntity.getMeetingtypeJg())) {
                meetingtypeEntity.setMeetingtypeJg(dictionaryService.queryBydictionaryValue(meetingtypeEntity.getMeetingtypeJg(), "nsjgmc"));
            }


        }

        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{meetingtypeId}")
//    @RequiresPermissions("archive:meetingtype:info")
    public R info(@PathVariable("meetingtypeId") Long meetingtypeId) {
        MeetingtypeEntity meetingtype = meetingtypeService.getById(meetingtypeId);
        if(StringUtils.isNotBlank(meetingtype.getFoursxlx()) && meetingtype.getFoursxlx().length()>10){
             //填充jgsyName
            String jgsyNameByCode = this.meetingtypeService.getJgsyNameByCode(meetingtype.getFoursxlx());
            meetingtype.setJgsyName(jgsyNameByCode);
        }
        return R.ok().put("meetingtype", meetingtype);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
//    @RequiresPermissions("archive:meetingtype:save")
    public R save(@RequestParam Map<String, Object> params) {
        MeetingtypeEntity meetingtype=new MeetingtypeEntity();
        meetingtype.setMeetingtypeNd((String) params.get("meetingtypeNd"));
        meetingtype.setMeetingtypeJg((String) params.get("meetingtypeJg"));
        meetingtype.setMeetingtypeName((String) params.get("meetingtypeName"));
        Long menuId = null;
        Object value = params.get("menuId");
        if (value != null) {
            try {
                menuId = Long.valueOf(value.toString()); // 尝试将值转换为Long类型
            } catch (NumberFormatException e) {
                // 处理转换错误的情况，例如记录错误或返回错误消息给调用者
                System.err.println("无法将menuId转换为Long类型: " + e.getMessage());
            }
        } else {
            // 处理"menuId"键不存在或其值为null的情况
            System.err.println("menuId键不存在或其值为null");
        }
        List<ArchiveCollectEntity> menuList=archiveCollectService.queryLocalMenu(params);
        String onesxlx="";
        String twosxlx="";
        String threesxlx="";

             onesxlx= menuList.get(menuList.size()-1).getValue();
             twosxlx= menuList.get(menuList.size()-2).getValue();
             threesxlx= menuList.get(0).getValue();


        meetingtype.setOnesxlx(onesxlx);
        meetingtype.setTwosxlx(twosxlx);
        meetingtype.setThreesxlx(threesxlx);
        boolean result =meetingtypeService.save(meetingtype);
        ArchiveCollectEntity archiveCollectEntity=new ArchiveCollectEntity();
        if (result) {
        meetingtype =  meetingtypeService.getBaseMapper().selectById(meetingtype.getMeetingtypeId());
        archiveCollectEntity.setParentId(menuId);
        archiveCollectEntity.setName(meetingtype.getMeetingtypeName());
        archiveCollectEntity.setValue(meetingtype.getFoursxlx());
        archiveCollectEntity.setType(1);
        archiveCollectEntity.setIcon("log");
        archiveCollectEntity.setOrderNum(StringUtils.convertToIntAndRemoveLeadingZeros(meetingtype.getFoursxlx()));
        archiveCollectEntity.setLevel("4");

            boolean result2 =    archiveCollectService.save(archiveCollectEntity);
            if (result2) {
                NdJgMenuEntity ndJgMenuEntity=new NdJgMenuEntity();
                ndJgMenuEntity.setNd(meetingtype.getMeetingtypeNd());
                ndJgMenuEntity.setJg(meetingtype.getMeetingtypeJg());
                ndJgMenuEntity.setMenuId(archiveCollectEntity.getMenuId());
                ndJgMenuService.save(ndJgMenuEntity);
            }
        } else {
            System.out.println("保存失败");
        }
        return R.ok().put("newItemMenuId",archiveCollectEntity.getMenuId());
    }


    /**
     * 保存
     */
    @RequestMapping("/saveByLevel")
//    @RequiresPermissions("archive:meetingtype:save")
    public R saveByLevel(@RequestParam Map<String, Object> params) {
        MeetingtypeEntity meetingtype=new MeetingtypeEntity();
        meetingtype.setMeetingtypeNd((String) params.get("meetingtypeNd"));
        meetingtype.setMeetingtypeJg((String) params.get("meetingtypeJg"));
        meetingtype.setMeetingtypeName((String) params.get("meetingtypeName"));
        Long menuId = null;
        Object value = params.get("menuId");
        if (value != null) {
            try {
                menuId = Long.valueOf(value.toString()); // 尝试将值转换为Long类型
            } catch (NumberFormatException e) {
                // 处理转换错误的情况，例如记录错误或返回错误消息给调用者
                System.err.println("无法将menuId转换为Long类型: " + e.getMessage());
            }
        } else {
            // 处理"menuId"键不存在或其值为null的情况
            System.err.println("menuId键不存在或其值为null");
        }
        List<ArchiveCollectEntity> menuList=archiveCollectService.queryLocalMenu(params);
        String onesxlx="";
        String twosxlx="";
        String threesxlx="";

        onesxlx= menuList.get(menuList.size()-1).getValue();
        twosxlx= menuList.get(menuList.size()-2).getValue();
        threesxlx= menuList.get(menuList.size()-3).getValue();


        meetingtype.setOnesxlx(onesxlx);
        meetingtype.setTwosxlx(twosxlx);
        meetingtype.setThreesxlx(threesxlx);
        boolean result =meetingtypeService.save(meetingtype);
        ArchiveCollectEntity archiveCollectEntity=new ArchiveCollectEntity();
        if (result) {
            meetingtype =  meetingtypeService.getBaseMapper().selectById(meetingtype.getMeetingtypeId());
            ArchiveCollectEntity archiveCollectEntity1=archiveCollectService.getById(menuId);
            archiveCollectEntity.setParentId(archiveCollectEntity1.getParentId());
            archiveCollectEntity.setName(meetingtype.getMeetingtypeName());
            archiveCollectEntity.setValue(meetingtype.getFoursxlx());
            archiveCollectEntity.setType(1);
            archiveCollectEntity.setIcon("log");
            archiveCollectEntity.setOrderNum(StringUtils.convertToIntAndRemoveLeadingZeros(meetingtype.getFoursxlx()));
            archiveCollectEntity.setLevel("4");

            boolean result2 =    archiveCollectService.save(archiveCollectEntity);
            if (result2) {
                NdJgMenuEntity ndJgMenuEntity=new NdJgMenuEntity();
                ndJgMenuEntity.setNd(meetingtype.getMeetingtypeNd());
                ndJgMenuEntity.setJg(meetingtype.getMeetingtypeJg());
                ndJgMenuEntity.setMenuId(archiveCollectEntity.getMenuId());
                ndJgMenuService.save(ndJgMenuEntity);
            }
        } else {
            System.out.println("保存失败");
        }
        return R.ok().put("newItemMenuId",archiveCollectEntity.getMenuId());
    }



    /**
     * 修改
     */
    @RequestMapping("/update")
//    @RequiresPermissions("archive:meetingtype:update")
    public R update(@RequestBody MeetingtypeEntity meetingtype) {
        meetingtypeService.updateById(meetingtype);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
//    @RequiresPermissions("archive:meetingtype:delete")
    public R delete(@RequestBody Long[] meetingtypeIds) {
        meetingtypeService.removeByIds(Arrays.asList(meetingtypeIds));

        return R.ok();
    }

    /**
     * 获取机构名称
     */
    @RequestMapping("/getJgsyName")
//    @RequiresPermissions("archive:meetingtype:save")
    public R getJgsyName(@RequestBody JgsyCommonEntity jgsyCommonEntity) {
        List<JgsyCommonEntity> list = meetingtypeService.getJgsyName(jgsyCommonEntity.getJgsyName());
        return R.ok().put("jgsyList", list);
    }

}
